﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HA.ModFlow.Domain.FileLayer
{
    public class POutputControl : Package
    {
        public POutputControl()
        {
            this._fileType = FileType.OC;
            this.Nunit = 90;
            this.Fname = "solution.oc";
        }

        int _IHEDFM;

        /// <summary>
        /// 水头输出格式
        /// </summary>
        public int IHEDFM
        {
            get { return _IHEDFM; }
            set { _IHEDFM = value; }
        }

        int _CHEDFM;
        /// <summary>
        /// 保存水头的格式
        /// </summary>
        public int CHEDFM
        {
            get { return _CHEDFM; }
            set { _CHEDFM = value; }
        }

        int _IHEDUN=91;

        /// <summary>
        /// 输出水头文件号
        /// </summary>
        public int IHEDUN
        {
            get { return _IHEDUN; }
            set { _IHEDUN = value; }
        }

        int _IDDNFM;
        /// <summary>
        /// 降深输出格式
        /// </summary>
        public int IDDNFM
        {
            get { return _IDDNFM; }
            set { _IDDNFM = value; }
        }

        int _CDDNFM;

        /// <summary>
        /// 输出到文件的降深格式
        /// </summary>
        public int CDDNFM
        {
            get { return _CDDNFM; }
            set { _CDDNFM = value; }
        }

        int _IDDNUN=92;

        /// <summary>
        /// 输出降深的文件号
        /// </summary>
        public int IDDNUN
        {
            get { return _IDDNUN; }
            set { _IDDNUN = value; }
        }

        int _CBOUFM;

        /// <summary>
        /// 边界条件输出到文件的格式
        /// </summary>
        public int CBOUFM
        {
            get { return _CBOUFM; }
            set { _CBOUFM = value; }
        }

        int _IBOUUN=93;

        /// <summary>
        /// 边界条件输出文件号
        /// </summary>
        public int IBOUUN
        {
            get { return _IBOUUN; }
            set { _IBOUUN = value; }
        }

        int _COMPACTBUGET;

        public int COMPACTBUGET
        {
            get { return _COMPACTBUGET; }
            set { _COMPACTBUGET = value; }
        }



        //逐层输出部分


        List<SPOutput> _periods = new List<SPOutput>();

        /// <summary>
        /// 逐个应力期的单独输出设置
        /// </summary>
        public List<SPOutput> Periods
        {
            get { return _periods; }

        }






        private string[] arryStr = { "(10G11.4)", "(11G10.3)", "(9G13.6)", "(15F7.1)", "(15F7.2)", "(15F7.3)",
                                     "(15F7.4)","(20F5.0)","(20F5.1)","(20F5.2)","(20F5.3)","(20F5.4)","(10G11.4)",
                                    "(10F6.0)","(10F6.1)","(10F6.2)","(10F6.3)","(10F6.4)","(10F6.5)","(5G12.5)","(6G11.4)","(7G9.2)"};


        public override void WriteFile(string path)
        {
            string content = "# HuanAnKeJi OC File" + "\r\n";
            content += "HEAD PRINT FORMAT  " + this.IHEDFM + "\r\n";
            //content += "HEAD SAVE FORMAT  " + arryStr[this.CHEDFM] + "\r\n";
            //content += "HEAD SAVE FORMAT  (10F12.5)"  + "\r\n";
            content += "HEAD SAVE UNIT  " + this.IHEDUN + "\r\n";
            content += "DRAWDOWN PRINT FORMAT  " + this.IDDNFM + "\r\n";
            //content += "DRAWDOWN SAVE FORMAT  " + arryStr[this.CDDNFM] + "\r\n";
            //content += "DRAWDOWN SAVE FORMAT  (10F12.5)" + "\r\n";
            content += "DRAWDOWN SAVE UNIT  " + this.IDDNUN + "\r\n";
            //content += "IBOUND SAVE FORMAT  " + arryStr[this.CBOUFM] + "\r\n";
            content += "IBOUND SAVE FORMAT  (10G12.5)" + "\r\n";
            content += "IBOUND SAVE UNIT  " + this.IBOUUN + "\r\n";
            //content += "IBOUND SAVE UNIT  " + this.IBOUUN + "\r\n";
            string str = "";
            for (int i = 0; i < this.Periods.Count; i++)
            {
                string step = "PERIOD " + (i + 1).ToString() + " STEP    ";               
                for (int j = 0; j < this.Periods[i].Steps.Count; j++)
                {
                    str += step + (j + 1).ToString() + "\r\n";                   
                    if (this.Periods[i].Steps[j].SaveBudget.BoutPut)
                    {
                        str += GetStr("    SAVE BUDGET", this.Periods[i].Steps[j].SaveBudget.Layers);

                        //if (this.Periods[i].Steps[j].SaveBudget.Layers == null)
                        //{
                        //    str += "    SAVE BUDGET" + "\r\n";
                        //}
                        //else
                        //{
                        //    if (this.Periods[i].Steps[j].SaveBudget.Layers.Count > 0)
                        //    {
                        //        for (int k = 0; k < this.Periods[i].Steps[j].SaveBudget.Layers.Count; k++)
                        //        {
                        //            str += "    SAVE BUDGET  " + this.Periods[i].Steps[j].SaveBudget.Layers[k] + "\r\n";
                        //        }
                        //    }
                        //    else
                        //    {
                        //        str += "    SAVE BUDGET" + "\r\n";
                        //    }
                        //}
                    }
                    if (this.Periods[i].Steps[j].SaveHead.BoutPut)
                    {
                        str += GetStr("    SAVE HEAD", this.Periods[i].Steps[j].SaveHead.Layers);                       
                    }
                    if (this.Periods[i].Steps[j].PrintBudget.BoutPut)
                    {
                        str += GetStr("    PRINT BUDGET", this.Periods[i].Steps[j].PrintBudget.Layers);    
                        //str += "   PRINT BUDGET" + "\r\n";
                    }
                    if (this.Periods[i].Steps[j].PrintHead.BoutPut)
                    {
                        str += GetStr("    PRINT HEAD", this.Periods[i].Steps[j].PrintHead.Layers);   
                        //str += "    PRINT HEAD" + "\r\n";
                    }
                    if (this.Periods[i].Steps[j].SaveDrawnDown.BoutPut)
                    {
                        str += GetStr("    SAVE DRAWDOWN", this.Periods[i].Steps[j].SaveDrawnDown.Layers);  
                        //str += "    SAVE DRAWDOWN" + "\r\n";
                    }
                    if (this.Periods[i].Steps[j].SaveIBound.BoutPut)
                    {
                        str += GetStr("    SAVE IBOUND", this.Periods[i].Steps[j].SaveIBound.Layers);  
                        //str += "    SAVE IBOUND" + "\r\n";
                    }
                }
            }
            content += str;
            string fname = path + this.Fname;
            HAUtil.FileOperations.WriteFile(fname, content);
        }

        private string GetStr(string str, List<int> Layer)
        {
            string mStr = "";
            if (Layer == null)
            {
                mStr += str + "\r\n";
            }
            else
            {
                if (Layer.Count > 0)
                {
                    for (int k = 0; k < Layer.Count; k++)
                    {
                        mStr += str + Layer[k] + "\r\n";
                    }
                }
                else
                {
                    mStr += str + "\r\n";
                }
            }
            return mStr;
        }
    }
}
